package com.mikufufu.blog.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.common.enums.FriendLinkStatus;
import com.mikufufu.blog.model.dto.admin.FriendLinkDto;
import com.mikufufu.blog.model.entity.FriendLink;
import com.mikufufu.blog.model.entity.LinkExtra;
import com.mikufufu.blog.model.excel.FriendLinkExcel;
import com.mikufufu.blog.service.FriendLinkService;
import com.mikufufu.blog.service.LinkExtraService;
import com.mikufufu.blog.service.excel.FriendLinkExcelListener;
import com.mikufufu.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/admin/friendLink")
@Api(value="友链", tags = "友链")
public class FriendLinkAdminController {

    @Autowired
    private FriendLinkService friendLinkService;

    @Autowired
    private LinkExtraService linkExtraService;

    @GetMapping("/detail")
    @ApiOperation("通过主键获取友链信息")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    public AjaxResult<FriendLink> detail(Integer id) {
        return AjaxResult.data(friendLinkService.getById(id));
    }

    @PostMapping("/page")
    @ApiOperation("分页获取友链列表")
    public AjaxResult<IPage<FriendLink>> page(@RequestBody FriendLinkDto friendLinkDto) {
        LambdaQueryWrapper<FriendLink> wrapper = new LambdaQueryWrapper<>();
        if (friendLinkDto.getStatus() != null) {
            wrapper.eq(FriendLink::getStatus, friendLinkDto.getStatus());
        }
        if (StringUtils.isNotBlank(friendLinkDto.getName())) {
            wrapper.like(FriendLink::getSiteName, friendLinkDto.getEmail());
        }
        if (StringUtils.isNotBlank(friendLinkDto.getEmail()) && StringUtils.isEmail(friendLinkDto.getEmail())) {
            wrapper.like(FriendLink::getEmail, friendLinkDto.getEmail());
        }
        return AjaxResult.data(friendLinkService.page(friendLinkDto.getPage(), wrapper));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增友链")
    public AjaxResult<Boolean> add(@RequestBody FriendLink friendLink) {
        return AjaxResult.data(friendLinkService.save(friendLink));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改友链")
    public AjaxResult<Boolean> update(@RequestBody FriendLink friendLink) {
        return AjaxResult.data(friendLinkService.updateById(friendLink));
    }

    /**
     * 审核通过
     * @param id 友链id
     * @return boolean
     */
    @PutMapping("/pass")
    @ApiOperation(value = "审核通过")
    public AjaxResult<Boolean> pass(Integer id) {
        FriendLink friendLink = friendLinkService.getById(id);
        friendLink.setStatus(FriendLinkStatus.PENDING.getStatus());
        return AjaxResult.data(friendLinkService.updateById(friendLink));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除友链")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.data(friendLinkService.removeById(id));
    }

    @PostMapping("/addExtra")
    @ApiOperation(value = "新增友链拓展信息")
    public AjaxResult<Boolean> addExtra(@RequestBody LinkExtra linkExtra) {
        return AjaxResult.data(linkExtraService.save(linkExtra));
    }

    @PutMapping("/updateExtra")
    @ApiOperation(value = "修改友链拓展信息")
    public AjaxResult<Boolean> updateExtra(@RequestBody LinkExtra linkExtra) {
        return AjaxResult.data(linkExtraService.updateById(linkExtra));
    }

    @DeleteMapping("/deleteExtra")
    @ApiOperation(value = "删除友链拓展信息")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Integer")
    public AjaxResult<Boolean> deleteExtra(Integer id) {
        return AjaxResult.data(linkExtraService.removeById(id));
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "导入友链")
    public AjaxResult<Boolean> importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), FriendLinkExcel.class, new FriendLinkExcelListener(friendLinkService)).sheet().doRead();
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("导入失败",e);
            return AjaxResult.error("导入失败");
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出友链数据")
    public void export(HttpServletResponse response) {
        friendLinkService.export(response);
    }

}
